<template>
  <div class="tags-add-container">
    <el-dialog
      :title="titleText"
      :visible.sync="dialogVisible"
      width="400px"
      :before-close="handleClose"
    >
      <el-form
        :inline="true"
        :model="tagsAddForm"
        size="small"
        label-width="80px"
        :rules="tagsRules"
        ref="tagsFormRef"
      >
        <el-form-item label="所属学科" v-if="!this.$route.query.id">
          <el-select
            v-model="tagsAddForm.subjectID"
            placeholder="请选择"
            class="tagsInput"
          >
            <el-option
              v-for="(item, index) in subAddList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- prop验证规则属性 -->
        <el-form-item label="目录名称" prop="tagName">
          <!-- v-model绑定到表单的数据项 -->
          <el-input
            v-model="tagsAddForm.tagName"
            placeholder="请输入目录名称"
            class="tagsInput"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('close')">取 消</el-button>
        <el-button type="primary" @click="tagsAddInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { simple } from '@/api/hmmm/subjects'
import { add, update } from '@/api/hmmm/tags'
export default {
  props: {
    tagsLists: {
      type: [Object],
      required: true
    },
    titleText: {
      type: [String],
      required: true
    },
    flag: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      // 控制对话框显示与隐藏
      dialogVisible: this.flag,
      subAddList: [], // 学科列表
      // 添加标签的表单数据
      tagsAddForm: {
        subjectID: this.tagsLists.subjectID || null, // 学科ID
        tagName: this.tagsLists.tagName || null, // 标签名称
        id: this.tagsLists.id || null
      },
      // 表单验证规则
      tagsRules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取学科数据
    this.getSubAddList()
  },
  methods: {
    // open() {
    //   this.dialogVisible = true
    // },
    // 获取学科数据
    async getSubAddList() {
      try {
        const { data: res } = await simple()
        // 获取的数据赋值学科列表
        this.subAddList = res
        console.log(res)
      } catch (err) {
        this.$message.error('获取学科失败！')
      }
    },
    // 提交确定
    tagsAddInfo() {
      // this.dialogVisible = false
      this.$refs.tagsFormRef.validate(async valid => {
        // console.log(valid) // 调用的结果
        if (valid) {
          if (this.tagsLists.id) {
            await update(this.tagsAddForm)
            this.$message.success('修改标签成功！')
            // console.log(valid)
          } else {
            console.log(this.$route.query, 111)
            if (this.$route.query.id) {
              this.tagsAddForm.subjectID = this.$route.query.id
            }
            await add(this.tagsAddForm)
            this.$message.success('新增标签成功！')
          }
          this.$emit('add')
          this.$emit('close')
        } else {
          this.$message.error('新增标签失败！')
        }
      })
    },
    handleClose() {
      this.$emit('close')
    }
  }
  // watch: {
  //   tagsLists: {
  //     handler() {
  //       // console.log(this.tagsLists, 22)
  //       this.tagsAddForm.id = this.tagsLists.id
  //       this.tagsAddForm.subjectID = this.tagsLists.subjectID
  //       this.tagsAddForm.tagName = this.tagsLists.tagName
  //     }
  //   }
  // }
}
</script>

<style scoped lang="scss">
.tags-add-container {
  ::v-deep.el-form-item {
    margin-right: 0;
  }
  .tagsInput {
    width: 280px;
  }
}
</style>
